﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.ComponentModel.Composition;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace Steve.UI.Extensions
{
    [Export(typeof(ILoggingService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class TraceContextLoggingService : ILoggingService
    {
        [Import]
        public IHttpContextBaseBuilder ContextBuilder { get; set; }

        public void LogEvent(LogEventType type, string source, int id, string message, Exception exceptionInfo)
        {
            var ctx = ContextBuilder.BuildCurrent();

            var msg = string.Format(CultureInfo.InvariantCulture, "{0}:{1}", id, message);

            switch(type)
            {
                case LogEventType.Info:
                    ctx.Trace.Write(source, msg, exceptionInfo);
                    break;
                default:
                    ctx.Trace.Warn(source, msg, exceptionInfo);
                    break;
            }
        }
    }
}
